package cn.schoolwow.quickdao.test.module.business.user.thread.flow;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.test.entity.Role;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

import java.util.List;

public class CheckThreadRequestResultFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkUser(flowContext);
        checkRole(flowContext);
    }

    @Override
    public String name() {
        return "检查多线程请求执行结果";
    }

    private void checkUser(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        int x = (int) flowContext.getData("x", 100);
        int y = (int) flowContext.getData("y", 10);

        int total = x*y;

        long count = dao.query(User.class).execute().count();
        Assert.assertEquals(total, count);
        List<String> usernameList = dao.query(User.class)
                .addColumn("username")
                .execute()
                .getSingleColumnList(String.class);
        for(int i=0;i<total;i++){
            if(!usernameList.contains("user"+i)){
                throw new AssertionError("用户表未包含数据:"+"user"+i);
            }
        }
    }

    private void checkRole(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        int x = (int) flowContext.getData("x", 100);

        long count = dao.query(Role.class).execute().count();
        Assert.assertEquals(x, count);
        List<String> roleNameList = dao.query(Role.class)
                .addColumn("roleName")
                .execute()
                .getSingleColumnList(String.class);
        for(int i=0;i<x;i++){
            Assert.assertTrue(roleNameList.contains("角色"+i));
        }
    }
}
